const mixin_formItemType = require('../../../../common/mixins/formItemType')
const mixin_dicKey = require('../../../../common/mixins/dicKey')
const {
  WxValidate
} = require('../../../../utils/WxValidate')
const {
  WorkOrderMyIndexApi
} = require('../../../../common/apis/workOrder')
Component({
  behaviors: [mixin_formItemType, mixin_dicKey],

  /**
   * 组件的属性列表
   */
  properties: {
    orderNum: String,
    assetName: String,
    assetAddr: String,
  },
  observers: {
    'assetAddr': function (val) {
      this.setData({
        equipmentAddressId: val
      })
    }
  },
  /** 
   * 组件的初始数据
   */
  data: {
    //  预计工时单位
    timeUnit: '小时',
    // 实际报修地址
    actualRepairPlace: '',
    // 资产名称
    equipment: '',
    // 资产位置
    equipmentAddress: '',
    // 资产位置id
    equipmentAddressId: '',
    // 故障描述
    faultRemark: '',
    // 故障分类
    faultType: '',
    // 处理时间
    makeTime: '',
    // 解决方法
    makeWay: '',
    // 订单号
    orderNum: '',
    // 附件
    processEnclosure: [],
    // 实际工时
    realTime: '',
    rules: {
      actualRepairPlace: {
        required: true,
      },
      faultRemark: {
        required: true,

      },
      faultType: {
        required: true,
      }
    },
    message: {
      actualRepairPlace: {
        required: '请选择报修地址'
      },
      faultRemark: {
        required: '请输入故障描述'
      },
      faultType: {
        required: '请选择故障分类'
      },

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //  监听附件的值
    onUploadData({
      detail
    }) {
      const {
        value,
        type
      } = detail
      if (type === 'fileList') {
        this.setData({
          processEnclosure: value
        })
      } else {
        let {
          processEnclosure
        } = this.data
        processEnclosure.push(...value)
        this.setData({
          processEnclosure: processEnclosure
        })
      }

    },
    submit(callback) {
      console.log(this.data, 'submit');
      const {
        actualRepairPlace,
        equipment,
        equipmentAddress,
        equipmentAddressId,
        faultRemark,
        faultType,
        makeTime,
        makeWay,
        orderNum,
        processEnclosure,
        realTime,
      } = this.data
      const params = {
        actualRepairPlace,
        equipment,
        equipmentAddress,
        equipmentAddressId,
        faultRemark,
        faultType,
        makeTime,
        makeWay,
        orderNum,
        processEnclosure,
        // 转换为小时
        realTime: this.data.timeUnit === '分钟' ? realTime / 60 : realTime,
      }
      const validate = new WxValidate(this.data.rules, this.data.message)
      if (!validate.checkForm(params)) {
        const error = validate.errorList[0]
        wx.showToast({
          title: error.msg,
          icon: 'error'
        })
        return false
      }
      WorkOrderMyIndexApi.complete(params, ({
        code,
        msg
      }) => {
        if (code === 200) {
          wx.showToast({
            title: '处理成功',
            icon: "success"
          })
          wx.$_.delay(callback, 1000)

        } else {
          console.error(msg);
          wx.showToast({
            title: msg,
            icon: 'error'
          })
        }
      })

    },

    onSelectTimeUint() {
      const units = ['小时', '分钟']
      const self = this
      wx.showActionSheet({
        itemList: units,
        success({
          tapIndex
        }) {
          self.setData({
            timeUnit: units[tapIndex]
          })
        }
      })
    },
  }
})